/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Entities;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import oracle.jdbc.pool.OracleDataSource;

/**
 *
 * @author JackOddie
 */
public class DBConnection {

    String jdbcUrl = "jdbc:oracle:thin:@localhost:1521:XE";
    String userid = "rpltr";
    String password = "rpltr";
    Connection conn;
    Statement stmt;
    ResultSet rset;
    String query;

    public DBConnection() {
    }

    public DBConnection(String jdbcUrl, String userid, String password) {
        this.jdbcUrl = jdbcUrl;
        this.userid = userid;
        this.password = password;
    }

    public void getDBConnection() throws SQLException {
        OracleDataSource ds;
        ds = new OracleDataSource();
        ds.setURL(jdbcUrl);
        conn = ds.getConnection(userid, password);
    }

    public ResultSet select(String query) throws SQLException {
        if(conn==null)
        this.getDBConnection();
        
        stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
        rset = stmt.executeQuery(query);
        return rset;
    }


    public void updateData(String query) throws SQLException {
        if(conn==null)
        this.getDBConnection();
        
        stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
        stmt.executeUpdate(query);
        
    }    
    
    public ResultSet getLast( String labelName, String TableName ) throws SQLException{
        String query = "select max("+labelName+") from "+TableName ;
        return select(query);
    }
 

    
    public ResultSet count(String labelName, String TableName) throws SQLException{
        String query = "select count(*) as "+labelName+" from "+TableName ;
        return select(query);
    }


    public void execute(String query) throws SQLException {
        if(conn==null)
        this.getDBConnection();
        
        stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
        stmt.execute(query);
    }
    public void close (){
        try {
            conn.close();
        } catch (SQLException ex) {
            Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}
